package com.leo.chapter14;

/**
 * 实现了阻塞的有界缓存，基于轮询和休眠
 * @author xuexiaolei
 * @version 2017年10月20日
 */
public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V> {
    protected SleepyBoundedBuffer(int capacity) {
        super(capacity);
    }

    @Override public void put(V v) throws InterruptedException {
        while (true){
            synchronized (this){
                if (!isFull()){
                    doPut(v);
                    return;
                }
            }
            Thread.sleep(10);
        }
    }

    @Override public V take() throws InterruptedException {
        while (true){
            synchronized (this){
                if (!isEmpty()){
                    return doTake();
                }
            }
            Thread.sleep(10);
        }
    }
}
